package com.ikkong.platform.controller;

import java.util.List;
import java.util.Map;

import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.platform.meta.intercept.BaseDocumentIntercept;
import com.ikkong.platform.model.BaseDocument;
import com.ikkong.platform.model.TemplateAttr;
import com.ikkong.platform.service.BaseDocumentService;
import com.ikkong.platform.service.TemplateAttrService;
import com.ikkong.platform.service.impl.BaseDocumentServiceImpl;
import com.ikkong.platform.service.impl.TemplateAttrServiceImpl;


/**
 * Generated by Blade.
 * 2018-06-21 15:11:21
 */
public class BaseDocumentController extends UrlPermissController {
	private static String CODE = "baseDocument";
	private static String PERFIX = "tb_base_document";
	private static String LIST_SOURCE = "BaseDocument.list";
	private static String BASE_PATH = "/platform/baseDocument/";
	
	BaseDocumentService service = new BaseDocumentServiceImpl();
	TemplateAttrService templateAttrService = new TemplateAttrServiceImpl();
	 
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "baseDocument.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "baseDocument_add.html");
	}

	public void edit() {
		String id = getPara(0);
		BaseDocument baseDocument = service.findById(id);
		setAttr("model", JsonKit.toJson(baseDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "baseDocument_edit.html");
	}

	public void view() {
		String id = getPara(0);
		BaseDocument baseDocument = service.findById(id);
		setAttr("model", JsonKit.toJson(baseDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "baseDocument_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new BaseDocumentIntercept());
		renderJson(grid);
	}
	
	@ClearShiro
	public void getTemplateAttrList() {
		int bank_id = getParaToInt("bank_id");
		int bussiness_id=getParaToInt("bussiness_id");
		//获取某银行业务下所有模板的占位符
		renderJson(error(SAVE_FAIL_MSG));
		List<TemplateAttr> templateAttrs = null;
		if (bank_id==0) {
			renderJson(error(SAVE_FAIL_MSG));
		}else{
			templateAttrs = templateAttrService.getTemplateAttrList(bank_id, bussiness_id);
			renderJson(templateAttrs);
		}
	}

	public void save() {
		BaseDocument baseDocument = mapping(PERFIX, BaseDocument.class);
		boolean temp = service.save(baseDocument);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		BaseDocument baseDocument = mapping(PERFIX, BaseDocument.class);
		//baseDocument.setVersion(baseDocument.getVersion()+1);
		boolean temp = service.update(baseDocument);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
